package hbase.mapReduce;

import java.io.IOException;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;

public class Reducer extends TableReducer<ImmutableBytesWritable, IntWritable, ImmutableBytesWritable> {

    public void reduce(ImmutableBytesWritable key, Iterable<IntWritable> values, Reducer.Context context)
            throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
        	System.out.println(val);
            sum += val.get();
        }
        System.out.println("---");
        Put put = new Put(key.get());
        put.add(Bytes.toBytes("date"), Bytes.toBytes("totalDocument"), Bytes.toBytes(sum));
        //System.out.println(String.format("stats :   key : %d,  count : %d", Bytes.toInt(key.get()), sum));
        System.out.println(String.format("stats :   count : %d", sum));
        context.write(key, put);
    }
}
